﻿Imports System.Data.SqlClient
Imports System.IO
Public Class clsCot_Anexos
    Private mintIdAnexo As Integer
    Private mintIdGeneral As Integer
    Private mstrAnexo As String
    Private mstrNombre As String
    Private mstrExtencion As String
    Private mstrDescripcion As String

    Public Property IdAnexo() As Integer
        Get
            Return mintIdAnexo
        End Get
        Set(ByVal value As Integer)
            mintIdAnexo = value
        End Set
    End Property
    Public Property IdGeneral() As Integer
        Get
            Return mintIdGeneral
        End Get
        Set(ByVal value As Integer)
            mintIdGeneral = value
        End Set
    End Property
    Public Property Anexo() As String
        Get
            Return mstrAnexo
        End Get
        Set(ByVal value As String)
            mstrAnexo = value
        End Set
    End Property
    Public Property Nombre() As String
        Get
            Return mstrNombre
        End Get
        Set(ByVal value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Extencion() As String
        Get
            Return mstrExtencion
        End Get
        Set(ByVal value As String)
            mstrExtencion = value
        End Set
    End Property
    Public Property Descripcion() As String
        Get
            Return mstrDescripcion
        End Get
        Set(ByVal value As String)
            mstrDescripcion = value
        End Set
    End Property
    Public Sub New()
        mintIdAnexo = 0
        mintIdGeneral = 0
        mstrAnexo = ""
        mstrNombre = ""
        mstrExtencion = ""
        mstrDescripcion = ""
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Cot_Anexos (" & _
                            "IdGeneral," & _
                            "Anexo," & _
                            "Nombre," & _
                            "Extencion," & _
                            "Descripcion)" & _
                            "VALUES (" & _
                            "@IdGeneral," & _
                            "@Anexo," & _
                            "@Nombre," & _
                            "@Extencion," & _
                            "@Descripcion)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim MyFile As FileStream
        MyFile = File.OpenRead(Anexo)
        Dim Arr(MyFile.Length) As Byte
        MyFile.Read(Arr, 0, MyFile.Length)


        'Dim FS As New FileStream(Anexo, FileMode.OpenOrCreate, FileAccess.Read)
        'Dim MyData(FS.Length) As Byte
        'FS.Read(MyData, 0, FS.Length)
        'FS.Close()

        Dim Command As New SqlCommand(sql, cnConn)
        Command.Parameters.AddWithValue("IdGeneral", mintIdGeneral)
        Command.Parameters.AddWithValue("Anexo", Arr)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Extencion", mstrExtencion)
        Command.Parameters.AddWithValue("Descripcion", mstrDescripcion)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el archivo anexo", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
